﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using syc.Helper;
using System.Drawing;

namespace syc.ZhiJian.Report
{
    public partial class 异议处理收拨平衡表 : System.Web.UI.Page
    {
        protected string year;
        protected string month;
        protected string RiQiStart;
        protected string RiQiEnd;
        protected string RiQi;
        protected DateTime start, end;
        private string sql = @"
SELECT dw.名称,
(SELECT SUM(重量)
FROM [SCM].[dbo].[原料异议库]
WHERE 日期<'2010-5-1' AND 
原料ID=1 AND
供货单位ID=y.[供货单位ID] AND
(已处理=0 or (已处理=1 and 处理日期>'2010-5-1'))
)AS 期初库存,
 (SELECT SUM(y2.[重量]) AS 重量
 FROM [原料异议库] as y2
 WHERE y2.[原料ID]=1 AND
	   y2.供货单位ID=y.[供货单位ID] AND
	   y2.[日期] between '2010-5-1' and '2010-05-30'
 ) AS 本期入库,
(SELECT SUM(y3.[重量]) AS 重量
 FROM [原料异议库] as y3
 WHERE y3.[原料ID]=1 AND
	   y3.已处理=1 AND
	   y3.供货单位ID=y.[供货单位ID] AND
	   y3.[处理日期] between '2010-5-1' and '2010-05-30'
 ) AS 本期出库,
(SELECT SUM(重量)
FROM [SCM].[dbo].[原料异议库]
WHERE 日期<'2010-6-1' AND 
原料ID=1 AND
供货单位ID=y.[供货单位ID] AND
(已处理=0 or (已处理=1 and 处理日期>'2010-6-1'))
) AS 期末库存
FROM [原料异议库] as y inner join [原料供货单位] as dw on
	 y.供货单位ID=dw.ID	
WHERE y.[原料ID]=1
GROUP BY y.供货单位ID,dw.名称";
        protected static DateTime date;
        protected static string fenchang;

        protected void Page_Load(object sender, EventArgs e)
        {
            this.Message.Text = "·准备就绪";
            fenchang = Request.QueryString["fenchang"];
            if (!this.IsPostBack)
            {

                //调用通用处理逻辑段【各个下拉列表变化时都调用了它】
                DList_month_SelectedIndexChanged(sender, e);
                Date1.TextChanged += new EventHandler(Date_TextChanged);
                Date2.TextChanged += new EventHandler(Date_TextChanged);
                
            }
            Date1.TextChanged += new EventHandler(DList_month_SelectedIndexChanged);
            Date2.TextChanged += new EventHandler(DList_month_SelectedIndexChanged);


        }
        //当两个日期框日期变化时调用此过程
        void Date_TextChanged(object sender, EventArgs e)
        {
            DList_month_SelectedIndexChanged(sender, e);
        }

        //各个下拉列表变化时都调用了它
        protected void DList_month_SelectedIndexChanged(object sender, EventArgs e)
        {
            
            bool success;
            success = DateTime.TryParse(Date1.Text, out start);
            switch (success)
            {
                case true:
                    RiQiStart = start.ToString("yyyy-MM-dd");
                    break;
                case false:
                    RiQiStart = DateTime.Now.ToString("yyyy-MM-dd");
                    Date1.Text = DateTime.Now.ToString("yyyy年MM月dd日");
                    break;
            }
            success = DateTime.TryParse(Date2.Text, out end);
            switch (success)
            {
                case true:
                    RiQiEnd = end.ToString("yyyy-MM-dd");
                    break;
                case false:
                    RiQiEnd = DateTime.Now.ToString("yyyy-MM-dd");
                    Date2.Text = DateTime.Now.ToString("yyyy年MM月dd日");
                    break;
            }
            
            sql = sql.Replace("*起始日期*", RiQiStart);
            sql = sql.Replace("*截止日期*", RiQiEnd);
            this.SqlDataSource1.SelectCommand = sql;
            this.ViewState["SelectCommand"] = sql;
            this.SqlDataSource1.SelectCommand = (string)this.ViewState["SelectCommand"];
        }


        //导出按钮
        protected void Button1_Click(object sender, EventArgs e)
        {
            //调用通用处理逻辑段【各个下拉列表变化时都调用了它】
            DList_month_SelectedIndexChanged(sender, e);
            //var ss = GridViewExportHelper.GetExportGridContent(GridView1, false,false);
            //GridViewExportHelper.WriteResponse(ss, "application/vnd.ms-excel", true, "Filename.xls");
            Response.Clear();
            Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
            Response.Charset = "utf-8";
            // If you want the option to open the Excel file without saving than
            // comment out the line below
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.ContentType = "application/vnd.xls";
            //设置EXCEL的表头信息
            var title = Date1.Text + "到" + Date2.Text + "入库明细";
            HttpContext.Current.Response.Write(
    @"<!DOCTYPE html PUBLIC ""-//W3C//DTD XHTML 1.0 Transitional//EN"" ""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"">
<html>
<head>
<meta http-equiv=""Content-Language"" content=""zh-cn""/>
<meta http-equiv=""Content-Type"" content="""
    + HttpContext.Current.Response.ContentType + "; charset=utf-8\"/></head><body><h2><center>" + title + "</center></h2>");
            System.IO.StringWriter stringWrite = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
            GridView1.RenderControl(htmlWrite);
            Response.Write(stringWrite.ToString());
            HttpContext.Current.Response.Write("</body></html>");
            Response.End();

        }

        //为了导出而重载的一个事件，具体看EXPORT TO EXCEL的文章
        public override void VerifyRenderingInServerForm(Control control)
        {

            // Confirms that an HtmlForm control is rendered for the
            //specified ASP.NET server control at run time.

        }

        //生成报表按钮
        protected void Query_Click(object sender, EventArgs e)
        {
            //调用通用处理逻辑段【各个下拉列表变化时都调用了它】
            DList_month_SelectedIndexChanged(sender, e);
        }

    }
}
